import 'package:mall/utils/sc/state_controller.dart';

import '../model/goods_detail_entity.dart';
import '../model/goods_entity.dart';
import '../model/project_selection_detail_entity.dart';
import '../service/goods_service.dart';
import '../service/project_selection_service.dart';

class SearchGoodsListSc extends PagingListSC<GoodsEntity> {
  String? keyword;
  String? sortName;
  String? orderType;

  @override
  Future<List<GoodsEntity>> loadData() async {
    final res = await GoodsService.searchGoods(
      keyword: keyword,
      pageIndex: page,
      limit: limit,
      sortName: sortName,
      orderType: orderType,
    );

    count = res.total;
    return res.xList ?? [];
  }
}

class CategoryGoodsListSc extends PagingListSC<GoodsEntity> {
  int? categoryId;

  @override
  Future<List<GoodsEntity>> loadData() async {
    if (categoryId == null) {
      throw Exception('category id should not be null');
    }
    final res = await GoodsService.getCategoryGoodsData(categoryId!, page, limit);
    count = res.total;
    return res.list ?? [];
  }
}

class GoodsDetailSc extends ModelSC<GoodsDetailEntity> {
  int? goodsId;

  @override
  Future<GoodsDetailEntity> loadData() async {
    if (goodsId == null) {
      throw Exception('goods id should not be null');
    }
    final res = await GoodsService.getGoodsDetailData(goodsId!);
    return res;
  }
}

class RelatedGoodsSc extends ListSC<ProjectSelectionDetailTopic> {
  int? id;
  @override
  Future<List<ProjectSelectionDetailTopic>> loadData() async {
    if (id == null) {
      throw Exception('id should not be null');
    }
    final res = await ProjectSelectionService.fetchRecommendedGoods(id!);
    return res.xList ?? [];
  }
}
